package com.lagou.service.impl;

import com.lagou.base.StatusCode;
import com.lagou.dao.impl.CourseContentDaoImpl;
import com.lagou.pojo.Course;
import com.lagou.pojo.Course_Lesson;
import com.lagou.pojo.Course_Section;
import com.lagou.service.CourseContentService;
import com.lagou.utils.DateUtils;

import java.util.List;

public class CourseContentServiceImpl implements CourseContentService {

    @Override
    public List<Course_Section> findSectionAndLessonByCourseId(int courseId) {
        CourseContentDaoImpl courseContentDao = new CourseContentDaoImpl();

        List<Course_Section> sectionList = courseContentDao.findSectionAndLessonByCourseId(courseId);

        return sectionList;
    }

    @Override
    public Course findCourseByCourseId(int courseId) {
        CourseContentDaoImpl courseContentDao = new CourseContentDaoImpl();

        Course course = courseContentDao.findCourseByCourseId(courseId);

        return course;
    }

    @Override
    public String saveSection(Course_Section section) {
        CourseContentDaoImpl courseContentDao = new CourseContentDaoImpl();

        section.setCreate_time(DateUtils.getDateFormart());

        section.setUpdate_time(DateUtils.getDateFormart());

        section.setStatus(2); //状态，0:隐藏;1:待更新;2:已发布

        int row = courseContentDao.saveSection(section);

        if(row > 0) {
            return StatusCode.SUCCESS.toString();
        } else {
            return StatusCode.FAIL.toString();
        }
    }

    @Override
    public  String updateSection(Course_Section section) {
        CourseContentDaoImpl courseContentDao = new CourseContentDaoImpl();

        //1.补全章节信息
        String date = DateUtils.getDateFormart(); section.setUpdate_time(date);

        //2.调用Dao进行插入
        int i = courseContentDao.updateSection(section);

        //3.根据修改是否成功,封装对应信息
        if(i > 0){
            //保存成功
            String result = StatusCode.SUCCESS.toString();
            return result;
        }else{
            //保存失败
            String result = StatusCode.FAIL.toString(); return result;
        }
    }

    @Override
    public String updateSectionStatus(int id, int status) {
        CourseContentDaoImpl courseContentDao = new CourseContentDaoImpl();

        //调用Dao 修改状态
        int i = courseContentDao.updateSectionStatus(id,status);

        //根据修改是否成功,封装对应信息
        if(i > 0){
            String result = StatusCode.SUCCESS.toString();
            return result;
        }else{
            String result = StatusCode.FAIL.toString();
            return result;
        }
    }

    @Override
    public String saveLesson(Course_Lesson lesson) {
        CourseContentDaoImpl courseContentDao = new CourseContentDaoImpl();

        lesson.setCreate_time(DateUtils.getDateFormart());

        lesson.setUpdate_time(DateUtils.getDateFormart());

        lesson.setStatus(2); //状态，0:隐藏;1:待更新;2:已发布

        int row = courseContentDao.saveLesson(lesson);

        if(row > 0) {
            return StatusCode.SUCCESS.toString();
        } else {
            return StatusCode.FAIL.toString();
        }
    }

    @Override
    public String updateLesson(Course_Lesson lesson) {
        CourseContentDaoImpl courseContentDao = new CourseContentDaoImpl();

        //1.补全章节信息
        String date = DateUtils.getDateFormart();
        lesson.setUpdate_time(date);

        //2.调用Dao进行插入
        int i = courseContentDao.updateLesson(lesson);

        //3.根据修改是否成功,封装对应信息
        if(i > 0){
            //保存成功
            String result = StatusCode.SUCCESS.toString();
            return result;
        }else{
            //保存失败
            String result = StatusCode.FAIL.toString();
            return result;
        }
    }
}
